/*****************************************************************************************
 *
 *
 *****************************************************************************************/

#ifndef FILEADAPTER_MD5_WRAPPER_H_
#define FILEADAPTER_MD5_WRAPPER_H_

#include <string>
#include "md5.h"

namespace MD5Test {

class Md5Wrapper {
 public:
  /**
   * @brief         read file and calculate md5
   * @param         in_filename        the name of the file
   * @param         md5_str         buffer to store MD5 string
   * @param         total_size      how many bytes need to read from file,
   *                                if the size is 0, then read all file
   * @return        -1 on error, 0 on success
   */
  static int32_t calculate_file_md5(const std::string &in_filename, std::string &md5_str, uint64_t total_size = 0);

  /**
   * @brief         read data from a file and append it to a MD5 context to
   *                re-calculate the MD5
   * @param         md5_ctx         MD5 context
   * @param         in_filename        the name of the file
   * @param         total_size      how many bytes need to read from the file
   *                                if the size is 0, then read all file.
   * @return        -1 on error, 0 on success
   */
  static int32_t update_file_md5(Md5Ctx &out_md5_ctx, const std::string &in_filename, uint64_t total_size);

  /**
   * @brief         calculate the MD5 of a string
   * @param         str             the string required to calculate MD5
   * @param         md5_str         buffer to store MD5 string
   * @return        -1 on error, 0 on success
   */
  static int32_t calculate_string_md5(const std::string &in_str, std::string &md5_str);

  /**
   * @brief         calculate the MD5 of a data block
   * @param         in_buf             data block
   * @param         total_size      how many bytes in the data block
   *                                need to be used to calculate MD5
   * @param         md5_str         buffer to store MD5 string
   * @return        -1 on error, 0 on success
   */
  static int32_t calculate_buf_md5(const char *const in_buf, uint64_t total_size, std::string &md5_str);

  /**
   * @brief         get md5 from *.md5 or *.md5_2 file
   * @param         the             name of the file
   * @param         md5_str          to store MD5 string
   * @return        -1 on error, 0 on success
   */
  static int32_t get_md5_from_md5_file(const std::string &in_filename, std::string &md5_str);

  /**
   * @brief         file's md5, then compare with file.md5
   * @param         in_filename        the name of the file
   * @return        -1 on mismatch, 0 on match
   */
  static int32_t check_md5_file(const std::string &in_filename);
};

}  // namespace MD5Test

#endif  // FILEADAPTER_MD5_WRAPPER_H_
